package com.inossem.warehousestorage_inossem.activity.out_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.outStorage.ReturnOutStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageReturnListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.outStorage.OutStorageReturnListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

//免检入库
public class OutStorageReturnListActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView returnOutStorageList;

    private List<OutStorageReturnListBean.DataBean.ReturnOutStorageListBean> allData;

    private ReturnOutStorageListAdapter adapter;

    private String mPurchaseNumber;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_out_storage_return_list);
        initView(OutStorageReturnListActivity.this);
        initData(OutStorageReturnListActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        returnOutStorageList = (PullableListView) activity.findViewById(R.id.returnOutStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.returnOutStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText("采购退货");
        ActivityUtils.initBackAndFunction(activity, "出库", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        returnOutStorageList.setPullUpFlag(false);
        returnOutStorageList.setPullDownFlag(false);
        resetData(activity);
        setmScanListener((String scanResult, int length) -> {
            mPurchaseNumber = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity,mPurchaseNumber);
        }, QRCodeConstant.TYPE_PURCHASE_ORDER);
    }

    private void resetData(Context context) {
        mPurchaseNumber = "";
        allData = new ArrayList<>();
        addOutStorageReturnListData(context, allData, returnOutStorageList, true);
    }

    private void addOutStorageReturnListData(Context context, List<OutStorageReturnListBean.DataBean.ReturnOutStorageListBean> returnOutStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new ReturnOutStorageListAdapter(context, returnOutStorageList, mPurchaseNumber, (String purchaseNumber) -> {
                String checkResult = check(purchaseNumber);
                if (checkResult == null) {
                    mPurchaseNumber = purchaseNumber;
                    getData(context, mPurchaseNumber);
                } else {
                    Utils.middleToast(context, checkResult);
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private String check(String purchaseNumber) {
        String result = null;
        if (TextUtils.isEmpty(purchaseNumber)) {
            result = "销售单号不可以为空";
        }
        return result;
    }

    private void getData(Context context, String purchaseNumber) {
        OutStorageReturnListRequest.request(context, purchaseNumber, new GsonBeanListener<OutStorageReturnListBean>() {
            @Override
            public void onSuccess(OutStorageReturnListBean bean) {
                allData = bean.getData().getReturnOutStorageList();
                if (allData.isEmpty()) {
                    Utils.middleToast(context, "未查到数据");
                }
                addOutStorageReturnListData(context, ActivityUtils.getShowData(allData, 0), returnOutStorageList, true);
            }
        });
    }

    public interface OutStorageReturnListener {
        void onNoDoubleClickListener(String pickingNumber);
    }

}
